package com.zy.controller;

import com.zy.bean.MUsers;
import com.zy.bean.Product;
import com.zy.service.IUserService;
import com.zy.service.impl.UserService;
import com.zy.utils.CommonUtil;
import com.zy.utils.ListenerTest;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author GongXl
 * @Description 用户登录
 * @Date 012 11:58
 * @Param
 * @return
**/
@WebServlet("/user")
public class UserController extends DispactherServlet {
    private IUserService iUserService = new UserService();
    /**
     * @Author GongXl
     * @Description 注销
     * @Date 022 15:14
     * @Param [req, res]
     * @return void
    **/
    public void logOut(HttpServletRequest req, HttpServletResponse res) throws IOException {
        req.getSession().invalidate();
        //登录页
        res.sendRedirect("index.jsp");
    }
    /**
     * @Author GongXl
     * @Description 登录京东
     * @Date 022 10:40
     * @Param [req, res]
     * @return void
    **/
    public void login(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException, SQLException, ClassNotFoundException {
        String userName = req.getParameter("userName");
        String passWord = req.getParameter("passWord");
        if(CommonUtil.isNotEmpty(userName) && CommonUtil.isNotEmpty(passWord)){
            //TODO 判断用户是否存在，存在就登录到主页，否则跳转登录页
            MUsers mUsers = new MUsers();
            mUsers.setUserName(userName);
            mUsers.setPassword(passWord);
            MUsers resMUsers = iUserService.queryUser(mUsers);
            req.getSession().setAttribute("muser",resMUsers);
            String jsssionId = req.getSession().getId();
            if(null != resMUsers ){
                //重定向可以避免重复提交
                //重定向，/ 代码tomcat webapps,跨项目
                res.sendRedirect("mall/test/jing_dong.jsp");
            }
        }
        //登录页
        res.sendRedirect("login.jsp");
    }
    public void show(HttpServletRequest req, HttpServletResponse res) throws IOException {
        res.setContentType("text/html;charset=utf-8");

        StringBuffer sb = new StringBuffer();
        sb.append("<html>");
        sb.append("<head>");
        sb.append("</head>");
        sb.append("<body>");
        sb.append("helllo");
        sb.append("</body>");
        sb.append("</html>");
        res.getWriter().write(sb.toString());
    }
    /**
     * @Author GongXl
     * @Description 登录
     * @Date 012 12:06
     * @Param [req, res]
     * @return void
    **/
    public void login_bak(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
        String userName = req.getParameter("userName");
        String passWord = req.getParameter("passWord");
        String[] hobbies = req.getParameterValues("hobbies");

        if(CommonUtil.isNotEmpty(userName) && userName.equals("admin")
                && CommonUtil.isNotEmpty(passWord) && passWord.equals("admin")){
            //登录成功
            System.out.println("登录成功");
            req.getSession().setAttribute("userName",userName);
            //模拟加载资源耗时
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            res.sendRedirect(req.getContextPath()+"/index.jsp");
        }else{
            //用户名或密码错误，请重新输入！
          List names = new ArrayList();
            names.add(0, "LiYang");
            names.add(1,"WangHua");
            req.setAttribute("list",names);

            Map namesm = new HashMap();
            namesm.put("one", "LiYang");
            namesm.put("two", "WangHua");
            req.setAttribute("map", namesm);

            List<Product> productList = new ArrayList<Product>();
            productList.add(new Product("苹果",10.1d,"云南"));
            productList.add(new Product("橘子",20.1d,"大理"));
            productList.add(new Product("火龙果",30.1d,"曲靖"));
            productList.add(new Product("苹果",10.1d,"云南"));
            productList.add(new Product("橘子",20.1d,"大理"));
            productList.add(new Product("火龙果",30.1d,"曲靖"));
            productList.add(new Product("苹果",10.1d,"云南"));
            productList.add(new Product("橘子",20.1d,"大理"));
            productList.add(new Product("火龙果",30.1d,"曲靖"));
            productList.add(new Product("苹果",10.1d,"云南"));
            productList.add(new Product("橘子",20.1d,"大理"));
            productList.add(new Product("火龙果",30.1d,"曲靖"));

            req.setAttribute("productList",productList);

            req.setAttribute("errorInfo","用户名或密码错误，请重新输入！");
            req.getRequestDispatcher("/error.jsp").forward(req,res);
        }
    }
    /**
     * @Author GongXl
     * @Description 注册用户
     * @Date 015 17:25
     * @Param [req, res]
     * @return void
    **/
    public void regUser(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
        String userName = req.getParameter("userName");
        String passWord = req.getParameter("passWord");
        //把用户写道数据库user表中
        System.out.println("添加了用户"+userName+"-"+passWord);
        res.sendRedirect("index.jsp");
    }
    /**
     * @Author GongXl
     * @Description 注册
     * @Date 012 12:07
     * @Param [req, res]
     * @return void
    **/
    public void testThymeleaf(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
        TemplateEngine engine = ListenerTest.getTemplateEngine(req.getServletContext());
        WebContext context = new WebContext(req, res, req.getServletContext());
        res.setCharacterEncoding("utf-8");
        context.setVariable("thymeleaf", "first  thymeleaf page!!");

        Product pro = new Product("苹果",10.1d,"云南");
        req.setAttribute("pro",pro);

        engine.process("regUser", context, res.getWriter());

    }
    /**
     * @Author GongXl
     * @Description 获得部分的子列表
     * @Date 007 16:55
     * @Param [req, res]
     * @return void
    **/
    public void getDepChildren(HttpServletRequest req, HttpServletResponse res) throws IOException {
        String depId = req.getParameter("depId");
        //查询出id的子类表
        System.out.println("#getDepChildren depId:"+depId);
        res.setContentType("text/html;charset=utf-8");

        StringBuffer sb = new StringBuffer();
//        {"msg":"success","status":200,"list":[{"depId":101,"name":"深圳总公司"},{"depId":102,"name":"长沙分公司"}]}
        sb.append(" {\"msg\":\"success\",\"status\":200,\"list\":[{\"depId\":101,\"name\":\"深圳总公司\"},{\"depId\":102,\"name\":\"长沙分公司\"}]}");
        res.getWriter().write(sb.toString());

    }


}
